import sys
sys.path.append('/Users/chengyanan/PycharmProjects/kuai8_appium')
from appium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait  #等待的
from selenium.webdriver.support import expected_conditions as ES  #条件判断
from uitls.read_init import Read_Init
from uitls.get_by_local import GetByLocal
def get_diver():
    capabilities = {
        'platformName': 'Android',
        'deviceName': '127.0.0.1:62001',
        'app': '/Users/chengyanan/PycharmProjects/kuai8_appium/apk/kuai8.apk',
        'appActivity': 'com.yixia.videoeditor.home.ui.SplashActivity',
        # "automationName": "uiautomator2",  # 抓取tost必须加
        # 切换activity  appwaitActivity
        "noReset": "true",
    }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', capabilities)
    return driver


driver = get_diver()


# 获取屏幕宽高
def get_size():
    size = driver.get_window_size()
    width = size['width']
    height = size['height']
    return width, height


# 左滑
def swipe_left():
    x1 = get_size()[0] / 10 * 9
    y1 = get_size()[1] / 2
    x = get_size()[0] / 10
    driver.swipe(x1, y1, x, y1)


# 向右滑动
def swipe_right():
    x1 = get_size()[0] / 10
    y1 = get_size()[1] / 2
    x = get_size()[0] / 10 * 9
    driver.swipe(x1, y1, x, y1)


# 上滑
def swipe_up():
    x1 = get_size()[0] / 2
    y1 = get_size()[1] / 10 * 9
    y = get_size()[1] / 10
    driver.swipe(x1, y1, x1, y)


# 下滑
def swipe_down():
    x1 = get_size()[0] / 2
    y1 = get_size()[1] / 10 * 9
    y = get_size()[1] / 10
    driver.swipe(x1, y1, x1, y)


def swipe_on(direction):
    if direction == 'up':
        swipe_up()
    elif direction == 'down':
        swipe_down()
    elif direction == 'left':
        swipe_left()
    else:
        swipe_left()


time.sleep(10)


def go_login():
    get_by_local=GetByLocal(driver)

    get_by_local.get_element('navbar').click()
    get_by_local.get_element('my').click()
    get_by_local.get_element('username').send_keys('13260310593')
    get_by_local.get_element('password').send_keys('1111')
    get_by_local.get_element('login_button').click()


def login_by_uiautomator():
    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.yixia.quick8:id/nu")').click()
    driver.find_element_by_android_uiautomator('new UiSelector().text("登录")').click()
    driver.find_element_by_android_uiautomator('new UiSelector().text("请输入手机号码")').send_keys('13260310593')
    driver.find_element_by_android_uiautomator('new UiSelector().text("请输入验证码")').send_keys('1111')
    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.yixia.quick8:id/vj")').click()

def login_by_xpath():
    driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.yixia.quick8:id/nu")').click()
    driver.find_element_by_xpath('//*[contains(@text,"登录")]').click()
    driver.find_element_by_xpath('//*[contains(@text,"请输入手机号码")]').send_keys('13260310593')
    driver.find_element_by_xpath('//*[contains(@text,"请输入验证码")]').send_keys('1111')
    driver.find_element_by_xpath('//android.widget.TextView[@text="登录"]').click()

def get_tost():
    driver.find_element_by_id('com.yixia.quick8:id/nu').click()
    driver.find_element_by_id('com.yixia.quick8:id/ro').click()
    driver.find_element_by_id('com.yixia.quick8:id/vg').send_keys('13260310593')
    driver.find_element_by_id('com.yixia.quick8:id/tc').send_keys('1411')
    driver.find_element_by_xpath('//android.widget.TextView[@text="登录"]').click()
    driver.find_element_by_xpath('//android.widget.TextView[@text="登录"]').click()
    tost_element=("xpath","//*[contains(@text,'验证码错误')]")
    loginButtonElement=WebDriverWait(driver,10,0.1).until(ES.presence_of_element_located(tost_element))
    print(loginButtonElement.text)


go_login()

if __name__ == '__main__':
    from business.login_business import Login_Business